package com.example.yskjproject.ui.fragment;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.viewbinding.ViewBinding;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.yskjproject.R;
import com.example.yskjproject.adapter.FollowAdapter;
import com.example.yskjproject.base.BaseFragment;
import com.example.yskjproject.content.Content;
import com.example.yskjproject.content.EventContent;
import com.example.yskjproject.databinding.FollowFragmentBinding;
import com.example.yskjproject.event.EventMessage;
import com.example.yskjproject.http.Urls;
import com.example.yskjproject.http.callback.JsonCallback;
import com.example.yskjproject.model.FollowDateModel;
import com.example.yskjproject.model.LzyResponse;
import com.example.yskjproject.view.FullyStaggeredGridLayoutManager;
import com.example.yskjproject.view.dialog.LoginWorkDialogHelper;
import com.google.common.collect.Lists;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

/**
 * FollowFragment
 * <p>
 * 作者：zx
 * 创建时间：2024/4/13
 */
public class FollowFragment extends BaseFragment {

    FollowFragmentBinding followFragmentBinding;
    private FollowAdapter followAdapter;

    private List<FollowDateModel.FollowListBean> listFollowCardDate = Lists.newArrayList();

    private LoginWorkDialogHelper loginWorkDialogHelper;

    @Override
    protected ViewBinding onCreateViewBinding(@NonNull LayoutInflater inflater, @Nullable ViewGroup parent) {
        followFragmentBinding = FollowFragmentBinding.inflate(inflater, parent, false);
        return followFragmentBinding;
    }

    @Override
    protected boolean isRegisterEventBus() {
        return true;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void MessageEvent(EventMessage eventMsg) {
        switch (eventMsg.getMessage()) {
            case EventContent.follow_list_data://刷新点赞列表
                getFollowDate();
                break;
        }
    }

    @Override
    protected void onData() {
        followFragmentBinding.title.titleTv.setText(getContext().getString(R.string.follow_title));
        followFragmentBinding.rvFollow.setLayoutManager(new FullyStaggeredGridLayoutManager(3, FullyStaggeredGridLayoutManager.VERTICAL));
        followAdapter = new FollowAdapter(getActivity(), listFollowCardDate,followFragmentBinding.rvFollow.getWidth());
        followFragmentBinding.rvFollow.setAdapter(followAdapter);
        followFragmentBinding.rvFollow.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                followFragmentBinding.rvFollow.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                followAdapter.setRecyclerViewWidth(followFragmentBinding.rvFollow.getWidth());
            }
        });

        getFollowDate();
        followFragmentBinding.swipeRefreshLayout.setOnRefreshListener(this::getFollowDate);
        followAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            if (view.getId() == R.id.dis_look_works || view.getId() == R.id.card_image){
                loginWorkDialogHelper = new LoginWorkDialogHelper(getActivity(), listFollowCardDate.get(position).getId());
                loginWorkDialogHelper.show();
            }
        });

    }

    /**
     * 获取关注列表
     */
    @SuppressLint("NotifyDataSetChanged")
    private void getFollowDate() {
        new Thread(()-> OkGo.<LzyResponse<FollowDateModel>>get(Urls.FOLLOW_LIST + "?page=" + Content.page + "&limit=" + Content.limit).execute(new JsonCallback<LzyResponse<FollowDateModel>>() {
            @Override
            public void onSuccess(Response<LzyResponse<FollowDateModel>> response) {
                List<FollowDateModel.FollowListBean> followListDateModel = response.body().data.getFollow_list();
                if (followListDateModel.isEmpty()) {
                    return;
                }
                listFollowCardDate.clear();
                listFollowCardDate.addAll(followListDateModel);
                followAdapter.notifyDataSetChanged();
                followFragmentBinding.swipeRefreshLayout.setRefreshing(false);
            }
            @Override
            public void onError(Response<LzyResponse<FollowDateModel>> response) {
                super.onError(response);
                followFragmentBinding.swipeRefreshLayout.setRefreshing(false);
            }
        })).start();
    }
}
